package cn.schoolwow.quickdao.test.module.business.user.dml.user.flow.edit.common;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.test.entity.User;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.instanceList.QueryInstanceList;
import com.alibaba.fastjson.TypeReference;
import org.junit.Assert;

import java.util.List;

public class CheckUpdateUserResultFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        checkInsertUser(flowContext);
        checkInsertUserList(flowContext);
    }

    @Override
    public String name() {
        return "检查更新用户结果";
    }

    private void checkInsertUser(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        User insertUser = flowContext.checkData("insertUser", User.class);

        String password = dao.query(User.class)
                .addQuery("id", insertUser.getId())
                .addColumn("password")
                .execute()
                .getSingleColumn(String.class);
        Assert.assertEquals(insertUser.getPassword(), password);
    }

    private void checkInsertUserList(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        List<User> insertUserList = flowContext.checkData("insertUserList", new TypeReference<List<User>>(){});

        List<Long> idList = QueryInstanceList.newQuery(insertUserList)
                .execute()
                .getSingleFieldValueList("id");
        List<User> userList = dao.query(User.class)
                .addInQuery("id", idList)
                .addColumn("id")
                .addColumn("password")
                .execute()
                .getList();
        for(User user:userList){
            User insertUser = QueryInstanceList.newQuery(insertUserList)
                    .addQuery("id", user.getId())
                    .execute()
                    .getOne();
            Assert.assertEquals(insertUser.getPassword(), user.getPassword());
        }
    }
}
